package com.miguo.component.search;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.miguo.app.AppConfig;
import com.miguo.app.AppContext;
import com.miguo.app.AppHelper;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.request.RequestJsonHandler;
import com.miguo.framework.view.tagview.Tag;
import com.miguo.framework.view.tagview.TagListView;
import com.miguo.framework.view.tagview.TagView;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.plugins.alibaba.BaiChuan;
import com.miguo.ui.R;

import java.util.ArrayList;
import java.util.List;

/**
 * File Names: SearchContentView
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-07-27
 * Time: 21:33
 */
public class SearchContentView extends LinearLayout {

    private Context mContext;
    private Activity mActivity;
    private TagListView mHotTagsView;
    private final List<Tag> mHotTagsList = new ArrayList<Tag>();

    public SearchContentView(Context context) {
        this(context, null);
    }

    public SearchContentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mActivity = (Activity) context;
        inflate(context, R.layout.layer_search_content_view, this);
        findView();
        initView();
    }


    private void findView() {
        mHotTagsView = (TagListView) findViewById(R.id.search_hot_tags_view);
        //search_history_view = (SearchHistoryView) findViewById(R.id.search_history_view);
    }

    private void initView() {
        initTagsData();
    }

    private void initTags() {
        mHotTagsView.setTags(mHotTagsList);
        mHotTagsView.setOnTagClickListener(new TagListView.OnTagClickListener() {
            @Override
            public void onTagClick(TagView tagView, Tag tag) {
                if (AppContext.isAilError) {
                    AppHelper.StartWebView(mActivity, AppConfig.TAOCONFIG.SEARCH + tag.getTitle());
                } else {
                    BaiChuan.showPage(mActivity, tag.getTitle(), AppConfig.TAOCONFIG.SEARCH + tag.getTitle());
                }
            }
        });
    }

    private void initTagsData() {
        RequestJsonHandler handler = new RequestJsonHandler() {
            @Override
            public void onRequestFinish(JsonData json) {
                if (json.optInt("status") == 200) {
                    JsonData data = json.optJson("data");
                    mHotTagsList.clear();
                    for (int i = 0; i < data.length(); i++) {
                        JsonData search = data.optJson(i);
                        Tag tag = new Tag();
                        tag.setId(i);
                        tag.setTitle(search.optString("name"));
                        mHotTagsList.add(tag);
                    }
                    initTags();
                }
            }
        };
        HttpClient.post(Url.toRoute(Url.Search.Keywords, 1), handler);
    }

}
